package com.dyb.idempotent.controller;

import com.dyb.idempotent.annotation.ExtApiIdempotent;
import com.dyb.idempotent.entity.User;
import com.dyb.idempotent.service.RedisTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ProjectName: idempotent
 * @Package: com.dyb.idempotent.controller
 * @ClassName: TestController
 * @Author: Deng YuBin
 * @Description:
 * @Date: 2021/1/14 20:13
 * @Version: 1.0
 */
@RestController
public class TestController {

    @Autowired
    private RedisTokenService tokenService;


    @RequestMapping(value = "addUser", produces = "application/json;charset=utf-8")
    public String addUser(@RequestBody User user, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            return "请求参数错误！";
        }
        boolean tokenOk = tokenService.findToken(token);
        if (!tokenOk) {
            return "请勿重复提交!";
        }
        //执行正常的业务逻辑
        System.out.println("user info:" + user);
        return "添加成功!";
    }

    @RequestMapping(value = "getToken")
    public String getToken() {
        return tokenService.getToken();
    }


    @GetMapping("/see/{key}")
    public String see(@PathVariable String key){
        return tokenService.see(key);
    }

    @ExtApiIdempotent(type = "head")
    @RequestMapping(value = "addUser2", produces = "application/json;charset=utf-8")
    public String addUser2(@RequestBody User user) {
        //执行正常的业务逻辑
        System.out.println("user info:" + user);
        return "添加成功!!";
    }
}
